package com.wind.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wind.activity.ArticleActivity;
import com.wind.activity.PostMomentActivity;
import com.wind.activity.navActivity;
import com.wind.adapter.MoreTypeAdapter;
import com.wind.base.BaseFragment;
import com.wind.beans.DiaryBean;
import com.wind.beans.DiaryItemBean;
import com.wind.soblogin.HttpManager;
import com.debug.soblogin.R;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class OceanFragment extends BaseFragment {
    private ArrayList<DiaryItemBean> mData;
    private RecyclerView mList;
    private MoreTypeAdapter mAdapter;
    private TextView mToPostActivity;

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {

        return inflater.inflate(R.layout.fragment_ocean, container, false);
    }


    @Override
    protected void initView(View rootView) {

        mList = rootView.findViewById(R.id.more_type_list);
        mToPostActivity = rootView.findViewById(R.id.ready_post_moment);
        super.initView(rootView);
    }

    @Override
    protected void initListener() {
        mToPostActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PostMomentActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {


        mData = new ArrayList<>();

        HttpManager
                .getSobApi()
                .getDiaryList()
                .enqueue(new Callback<DiaryBean>() {
                    @Override
                    public void onResponse(Call<DiaryBean> call, Response<DiaryBean> response) {
                        DiaryBean data = response.body();
                        for (int i = 0; i < data.getData().getContent().size(); i++) {
                            DiaryItemBean kk = new DiaryItemBean();
                            ArrayList<String> PicList = new ArrayList<>();
                            kk.setCreateTime(data.getData().getContent().get(i).getCreateTime());
                            kk.setDiaryContent(data.getData().getContent().get(i).getContent());
                            int imageNum = data.getData().getContent().get(i).getImageNum();
                            if (imageNum == 0) {
                                PicList.add(data.getData().getContent().get(i).getCover1());
                                kk.setImageNum(0);

                            }
                            if (imageNum == 1) {
                                PicList.add(data.getData().getContent().get(i).getCover1());
                                kk.setImageNum(1);

                            }
                            if (imageNum == 2) {
                                PicList.add(data.getData().getContent().get(i).getCover1());
                                PicList.add(data.getData().getContent().get(i).getCover2());
                                kk.setImageNum(2);
                            }
                            if (imageNum == 3) {
                                PicList.add(data.getData().getContent().get(i).getCover1());
                                PicList.add(data.getData().getContent().get(i).getCover2());
                                PicList.add(data.getData().getContent().get(i).getCover3());
                                kk.setImageNum(3);
                            }
                            //首先看获得的number是多少
                            kk.setPicList(PicList);
                            mData.add(kk);
                        }
                        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                        mList.setLayoutManager(layoutManager);

                        //创建适配器
                        mAdapter = new MoreTypeAdapter(mData);
                        //设置到recycle里面去
                        mList.setAdapter(mAdapter);


                    }

                    @Override
                    public void onFailure(Call<DiaryBean> call, Throwable t) {
                        Toast.makeText(getContext(), "失败", Toast.LENGTH_LONG).show();
                        System.out.println("---------->出错了" + call + t);
                    }
                });
    }


}
//                for (int i = 0; i < data.getData().getContent().size(); i++) {
//        DiaryItemBean kk = new DiaryItemBean();
//        ArrayList<String> PicList = new ArrayList<>();
//        kk.setDiaryContent(data.getData().getContent().get(i).getContent());
//        int imageNum = data.getData().getContent().get(i).getImageNum();
//        if (imageNum == 1) {
//        PicList.add(data.getData().getContent().get(i).getCover1());
//
//        }
//        if (imageNum == 2) {
//        PicList.add(data.getData().getContent().get(i).getCover1());
//        PicList.add(data.getData().getContent().get(i).getCover2());
//        }
//        if (imageNum == 3) {
//        PicList.add(data.getData().getContent().get(i).getCover1());
//        PicList.add(data.getData().getContent().get(i).getCover2());
//        PicList.add(data.getData().getContent().get(i).getCover3());
//        }
//        //首先看获得的number是多少
//        kk.setPicList(PicList);
//        mData.add(kk);
//        }